/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package slicktest.factories.Readers;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import slicktest.types.TagStatusType;

/**
 *
 * @author hailstorm
 */
public class SimpleTagReader implements TagReader {

    private File file;
    //whether the file parsing completed successfully, and if some tags
    //are missing.
    private TagStatusType status = TagStatusType.NOT_READ;
    private List<String> requiredTags;
    private List<String> optionalTags;
    private Map<String, String[]> requiredTokens;
    private Map<String, String[]> optionalTokens;

    public SimpleTagReader() {
        requiredTags = new ArrayList<String>();
        optionalTags = new ArrayList<String>();

        requiredTokens = new HashMap<String, String[]>();
        optionalTokens = new HashMap<String, String[]>();
    }

    public Map<String, String[]> getRequiredTokens() {
        return requiredTokens;
    }

    public Map<String, String[]> getOptionalTokens() {
        return optionalTokens;
    }

    public void setRequiredTags(List<String> tags) {
        this.requiredTags = tags;
    }

    public void setOptionalTags(List<String> tags) {
        this.optionalTags = tags;
    }

    public TagStatusType getStatus() {
        return status;
    }

    public void setTargetFile(File file) {
        this.file = file;

        requiredTokens.clear();
        optionalTokens.clear();

        System.out.println("setting file to: " + file.getPath());
        status = TagStatusType.NOT_READ;

        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));

            //use this to flag whether the file is a valid one. things that invalidate:
            //missing required tag
            //token has no values
            boolean stillValid = true;

            while (reader.ready() && stillValid) {

                String nextLine = reader.readLine();
                String[] tokens = nextLine.split("\\s+");
                String tag = "";

                System.out.println("\n" + "next line: " + nextLine);

                if (tokens.length > 1) {
                    tag = tokens[0];
                } else {
                    stillValid = false;
                    System.out.println("No tag found, or only tag is present for: " + nextLine);
                }

                if (stillValid) {
                    if (requiredTags.contains(tag)) {
                        System.out.println("Added to required listing");
                        requiredTokens.put(tag, tokens);
                    } else if (optionalTags.contains(tag)) {
                        System.out.println("Added to optional listing");
                        optionalTokens.put(tag, tokens);
                    } else {
                        System.out.println("Discarding line: " + nextLine);
                    }
                }
            }

            System.out.println("\n" + "req tokens: " + requiredTokens);            
            System.out.println("req tags: " + requiredTags);
            
            System.out.println("\n" + "opt tokens: " + optionalTokens);
            System.out.println("opt tags: " + optionalTags);
            
            //check that all of the required tokens are present.
            System.out.println("\nchecking for required tags");

            for (int i = 0; i < requiredTags.size(); i++) {
                String nextTag = requiredTags.get(i);

                if (requiredTokens.containsKey(nextTag)) {
                    System.out.println(nextTag + " found");
                } else {
                    System.out.println(nextTag + " not found");
                    status = TagStatusType.MISSING_REQUIRED;
                    stillValid = false;
                }
            }

            if (stillValid) {
                status = TagStatusType.OK;
            }

        } catch (FileNotFoundException ex) {
            System.out.println(file.getPath() + " was not found, or is invalid");
            status = TagStatusType.FILE_NOT_FOUND;
        } catch (IOException ex) {
            System.out.println("IO Exception reading file");
            status = TagStatusType.IO_EXCEPTION;
        }
    }
}
